<template>
  <div class="content">
    <jc-button
      :btn-arr="['reload', bomCost('salPrice:save') && 'newy', 'remove', 'option', 'batchFill', 'sub', 'pass']"
      :disab-arr="['close', 'cancel', 'copy', 'chart', 'com1', 'com2', 'freeze']"
      url="tSalPricelist/querySalPriceList"
      @audit="audit(1)"
      @noAudit="audit(2)"
      @submit="subPrice(1)"
      @noSubmit="subPrice(2)"
      @forbidden="forbidden(1)"
      @startUsing="forbidden(2)"
      @refresh="handleQuerySonClass"
      @newly="addMateril"
      @del="delectHandle"
      @toExcel="handleExport"
    />
    <div class="header">
      <div class="header-name">
        <search
          :options="selectData"
          :search-son="searchSon"
          :msg="tacitly"
          @seek="collect"
          @hand="handleQuerySonClass"
        />
        <el-button
          type="primary"
          class="btn"
          size="mini"
          @click="handleQuerySonClass"
        >
          {{ $t('btn.search') }}
        </el-button>
        <filtration
          :ftype="ftype"
          :msg="tacitly"
          :table-header="tableHeader1"
          :detail-header="tableHeader"
          :select-data="selectData"
          @seachData="handleQuerySonClass"
        />
      </div>
    </div>
    <div class="table-content">
      <jc-table
        v-loading="loading"
        :table-data="tableData"
        :table-header="tableHeader"
        table-selection
        sortable
        get-row-keys="fentryId"
        @selectionChange="selectionData"
        @clickRow="particulars"
      ><!--    单据编码    -->
        <template v-slot:tagSlot="col">
          <el-link type="primary" @click="particulars(col.scope.row.fid)">
            {{ col.scope.row.fbillNo }}
          </el-link>
        </template>
        <!--    物料编码    -->
        <template v-slot:orderSlot="col">
          <el-link type="primary" @click="jumpMateriel(col.scope.row)">
            {{ col.scope.row.fnumber }}
          </el-link>
        </template>
        <template v-slot:btnState="col">
          <span v-if="col.scope.row.fdocumentStatus === ''" />
          <el-tag v-else :type="col.scope.row.stateColor">{{ col.scope.row.fdocumentStatus }}</el-tag>
        </template>
        <template v-slot:btnStates="clo">
          <el-tag v-if="clo.scope.row.fforbidStatus==='否'">{{ clo.scope.row.fforbidStatus }}</el-tag>
          <el-tag v-else type="danger">{{ clo.scope.row.fforbidStatus }}</el-tag>        </template>
      </jc-table>
    </div>
    <!--    分页器-->
    <div class="footer">
      <jc-pagination
        v-show="total > 0"
        :total="total"
        :page.sync="currentPage"
        :limit.sync="size"
        @pagination="handleGetUntreated"
      />
    </div>
  </div>
</template>

<script>
import {
  againReviewPrice,
  deleteSalPrice,
  notReviewPrice,
  querySalPriceList,
  reviewPrice, salPriceListDisable,
  salPriceListSubmit, salPriceListUnDisable
} from '@/api/marketManage/marketPriceList'
import search from '@/components/Search'
import searData from '@/components/Search/mixin'
import { Disable, toMxAmina } from '@/components/ToMxamineState'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import filtration from '@/components/filtration'
import jcButton from '@/components/Button'
export default {
  components: {
    jcButton,
    search,
    filtration
  },
  mixins: [searData],
  data() {
    return {
      ftype: 7,
      total: 0, // 总条目
      currentPage: 1, // 当前页
      size: 200, // 每页显示多少条数据
      selectFbi: [], // 审核数据
      // 表头
      tableData: [], // 销售数据
      tableHeader: [
        { label: this.$t('market.coding'), type: 'tag', prop: 'fbillNo', width: '150' },
        { label: this.$t('market.priceName'), prop: 'priceListName' },
        { label: this.$t('market.cust'), prop: 'fcustName' },
        { label: this.$t('market.currency2'), prop: 'fcurrency', width: '80' },
        { label: this.$t('market.documentstatus'), type: 'state', prop: 'fdocumentStatus', width: '100' },
        { label: this.$t('market.forbidStatu'), type: 'states', prop: 'fforbidStatus', width: '100' },
        { label: this.$t('market.fmodel'), prop: 'fmodel' },
        { label: this.$t('market.fmaterial'), type: 'order', prop: 'fnumber', width: '160' },
        { label: this.$t('shop.fname2'), prop: 'fname' },
        { label: this.$t('shop.fspecificaTion'), prop: 'fspecificaTion' },
        { label: this.$t('market.funit'), prop: 'funit', width: '70' },
        { label: this.$t('market.xxprice'), prop: 'fprice', align: 'right', width: '80' },
        { label: this.$t('market.effectiveDate'), prop: 'feffectivedate' },
        { label: this.$t('market.expiryDate'), prop: 'fexpriydate' },
        { label: this.$t('market.fdescripTion'), prop: 'fdescripTion', width: '200' }
      ],
      tableHeader1: [
        { label: this.$t('market.lowestPrice'), prop: 'fdownPrice', headAlign: 'center', align: 'right', width: '100' },
        { label: this.$t('market.priceCoeff'), prop: 'fpricebase' },
        { label: this.$t('market.lapsedPerson'), prop: 'fforbider' },
        { label: this.$t('market.lapsedExpiry'), prop: 'fforbidDate' },
        { label: this.$t('market.hangStates'), prop: 'frowauditStatus' },
        { label: this.$t('market.note'), prop: 'fnote' },
        { label: this.$t('market.creator'), prop: 'fcreator' },
        { label: this.$t('market.createDate'), prop: 'fcreateDate' },
        { label: this.$t('market.lastmodifier'), prop: 'fmodifier' },
        { label: this.$t('market.lastmodifyDate'), prop: 'fmodifyDate' },
        { label: this.$t('market.approver'), prop: 'fapprover' },
        { label: this.$t('market.approveDate'), prop: 'fapproveDate' },
        { label: this.$t('market.forbider'), prop: 'fforbider' },
        { label: this.$t('market.forbidDate'), prop: 'fforbidDate' }
      ],
      selectVal: []
    }
  },
  created() {
    this.handleGetUntreated()
    this.tableHeader1 = this.tableHeader.concat(this.tableHeader1)
  },
  methods: {
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map(res => {
          return res.label
        })
        const filterVal = this.tableHeader.map(res => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else if (this.val && this.val.length) {
          const list = this.val.map(item => {
            return (toMxAmina(item), Disable(item))
          })
          const data = formatJson(filterVal, list)
          export_json_to_excel(tHeader, data, '销售客户价目表')
        } else {
          querySalPriceList({
            pageSize: 100000,
            pageNum: 1,
            search: this.searCollData
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.array
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '销售客户价目表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 多选
    selectionData(val) {
      this.val = val
      this.selectVal = []
      this.selectFbi = []
      if (val.length > 0) {
        val.forEach(res => {
          res.fbillNo && this.selectFbi.push(res.fbillNo)
          this.selectVal.push({
            fid: res.fid,
            fnumber: res.fbillNo,
            fdocumentStatus: res.fdocumentStatus
          })
        })
      }
    },
    // 获取销售价目列表数据
    async handleGetUntreated(ev) {
      this.loading = true
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = {
        pageNum: this.currentPage,
        pageSize: this.size,
        search: this.searCollData.concat(this.details)
      }
      const { data: RES, data: total } = await querySalPriceList(DATA)
      this.tableData = RES.array
      this.tableData.map(item => {
        return (toMxAmina(item), Disable(item))
      })
      this.total = total.total
      this.loading = false
    },
    // 搜索
    handleQuerySonClass(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.customHandle(ev)
        this.handleGetUntreated(ev.detail)
      } else {
        this.handleGetUntreated()
      }
    },
    // 价目详情
    particulars(ev) {
      const id = ev.fid || ev
      this.$router.push({ path: `/markedEdit/${id}` })
    },
    // 进入物料详情
    jumpMateriel(ev) {
      const FNUMBER = ev.fnumber
      this.$router.push({ path: `/detailMateriel/${FNUMBER}` })
    },
    // 新增价目
    addMateril() {
      this.$router.push({ path: '/createMarketPrice' })
    },
    // 公共函数
    toExamineFun(api) {
      const DATA = { Numbers: this.selectFbi }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.handleGetUntreated()
        this.selectFbi = []
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
      })
    },
    // 审核
    audit(ev) {
      if (this.selectFbi.length === 0) {
        this.$message({
          showClose: true,
          message: '请选择数据',
          type: 'warning'
        })
        return
      }
      const api = ev === 1 ? reviewPrice : notReviewPrice
      this.toExamineFun(api)
    },
    // 提交
    subPrice(ev) {
      const api = ev === 1 ? salPriceListSubmit : againReviewPrice
      this.toExamineFun(api)
    },
    // 禁用 反禁用
    forbidden(ev) {
      const api = ev === 1 ? salPriceListDisable : salPriceListUnDisable
      this.toExamineFun(api)
    },
    // 删除销售订单
    delectHandle() {
      if (this.selectFbi.length === 0) {
        this.$message.warning('请选择要删除的订单！')
        return
      }
      this.$confirm('此操作将永久删除该订单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const api = deleteSalPrice
        this.toExamineFun(api)
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.content {
  @include listBom;
  .header {
    position: relative;
    .header-name {
      width: 100%;
    }
    .btn {
      transform: translateY(18%);
      margin-left: 450px !important;
    }
  }
}
</style>
